package com.yhp.web;

import com.yhp.bean.Menu;
import com.yhp.bean.Role;
import com.yhp.service.MenuService;
import com.yhp.service.RoleService;
import com.yhp.service.impl.MenuServiceImpl;
import com.yhp.service.impl.RoleServiceImpl;
import com.yhp.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/power/role/roles")
public class RoleServlet extends HttpServlet {
    private RoleService roleService=new RoleServiceImpl();
    private MenuService menuService=new MenuServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if("select".equals(method)){
            select(req,resp);
        }else if("selectmenus".equals(method)){
            selectmenus(req,resp);
        }else if("insert".equals(method)){
            insert(req,resp);
        }else if ("finfbyid".equals(method)){
            finfbyid(req,resp);
        }else if ("update".equals(method)){
            update(req,resp);
        }else if ("info".equals(method)){
            info(req,resp);
        }else if ("delRole".equals(method)){
            delRole(req,resp);
        }else if ("statRole".equals(method)){
            statRole(req,resp);
        }
    }
    protected void statRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rid = req.getParameter("rid");
        String state = req.getParameter("state");
        RoleService roleService = new RoleServiceImpl();
        int i = roleService.statRole(Integer.parseInt(rid), Integer.parseInt(state));
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('设置成功');location.href='/power/role/roles?method=select'</script>");
        }else{
            writer.println("<script>alert('设置失败');location.href='/power/role/roles?method=select'</script>");
        }
    }
    protected void info(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rid = req.getParameter("rid");
        RoleService roleService = new RoleServiceImpl();
        Role role = roleService.findById(Integer.parseInt(rid));

        //获取菜单列表
        List<Menu> menuList = menuService.getMenuList();
        req.setAttribute("menulist",menuList);
        req.setAttribute("role", role);
        req.getRequestDispatcher("info.jsp").forward(req,resp);
    }
    //主键查询
    protected void finfbyid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rid = req.getParameter("rid");
        RoleService roleService = new RoleServiceImpl();
        Role role = roleService.findById(Integer.parseInt(rid));

        //获取菜单列表
        List<Menu> menuList = menuService.getMenuList();

        // 获取已经有的菜单列表
        req.setAttribute("menulist",menuList);
        req.setAttribute("role", role);
        req.getRequestDispatcher("edit.jsp").forward(req,resp);
    }
    //更新操作
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleid = req.getParameter("roleid");
        String rolename = req.getParameter("rolename");
        String state = req.getParameter("state");
        String[] menuids = req.getParameterValues("menuid");
        int i = roleService.updateRole(Integer.parseInt(roleid), rolename, state, menuids);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
//            resp.sendRedirect("/power/role/roles?method=select");
            writer.println("<script>alert('新增成功');location.href='/power/role/roles?method=select'</script>");
        }else{
//            resp.sendRedirect("/power/role/roles?method=selectmenus");
            writer.println("<script>alert('新增失败');location.href='/power/role/roles?method=update'</script>");
        }
    }

    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收参数(分页参数+模糊查参数)
        String index = req.getParameter("index");
        int pageIndex=(index==null||index.length()==0)?1:Integer.parseInt(index);
        //2.调取service方法(1.查询数据列表的方法  2.查询总条数的方法)
        PageUtil pageUtil = new PageUtil();
        List<Role> roleList = roleService.getRoleList(pageIndex, pageUtil.getPageSize());
        int total = roleService.total();
        pageUtil.setTotal(total);
        pageUtil.setPageIndex(pageIndex);
        pageUtil.setDataList(roleList);

        //3.存值跳页面
        req.setAttribute("pi",pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req,resp);

    }

    protected void selectmenus(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收参数
        //2.调取service方法
        List<Menu> menuList = menuService.getMenuList();
        req.setAttribute("menulist",menuList);
        req.getRequestDispatcher("add.jsp").forward(req,resp);
    }
    protected void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rolename = req.getParameter("rolename");
        String state = req.getParameter("state");
        String[] menuids = req.getParameterValues("menuid");
        int i = roleService.insertRole(rolename, state, menuids);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
//            resp.sendRedirect("/power/role/roles?method=select");
            writer.println("<script>alert('新增成功');location.href='/power/role/roles?method=select'</script>");
        }else{
//            resp.sendRedirect("/power/role/roles?method=selectmenus");
            writer.println("<script>alert('新增失败');location.href='/power/role/roles?method=selectmenus'</script>");
        }
    }
    //删除角色
    protected void delRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rid = req.getParameter("rid");
        RoleService roleService = new RoleServiceImpl();
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        int i = roleService.delRole(rid);
        if (i>0){
            writer.println("<script>alert('删除成功');location.href='/power/role/roles?method=select'</script>");
        }else {
            writer.println("<script>alert('删除失败');location.href='/power/role/roles?method=select'</script>");
        }

    }
}
